﻿using System.Drawing;
using System.IO;

using Microsoft.WowAddonStudio.FrameXml.Formats.Mpq;
using Microsoft.WowAddonStudio.FrameXml.Services;
using Microsoft.WowAddonStudio.FrameXml.Services.Tests.Properties;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.FrameXml.Services.Tests
{
    [TestFixture]
    public class FrameXmlFontServiceTest
    {
        private const string mpqArchiveRelativePath = @"Data\enGB\locale-enGB.MPQ";
        private const string mpqMorpheusFontPath = @"Fonts\MORPHEUS.TTF";
        private const string mpqSkurriFontPath = @"Fonts\SKURRI.TTF";

        private string mpqArchivePath;

        [SetUp]
        public void SetUp()
        {
            if (!Directory.Exists(Settings.Default.WowBasePath))
                Assert.Ignore("Cannot run test. Please set the World of Warcraft base path.");

            mpqArchivePath = Path.Combine(Settings.Default.WowBasePath, mpqArchiveRelativePath);
        }

        [Test]
        public void GetFontTest()
        {
            using (var mpqArchive = new MpqArchive(mpqArchivePath))
            using (var resourceFileLoader = new ResourceFileLoader())
            using (var frameXmlFontService = new FrameXmlFontService(resourceFileLoader))
            {
                // Add WoW base path
                resourceFileLoader.BasePaths.Add(Settings.Default.WowBasePath);

                // Add MPQ archive
                resourceFileLoader.MpqArchives.Add(mpqArchive);

                // Get the MPQ font 'Morpheus'
                FontFamily morpheusFontFamily = frameXmlFontService.GetFont(mpqMorpheusFontPath);

                Assert.IsNotNull(morpheusFontFamily, "GetFontFamily returned null.");
                Assert.AreEqual("Morpheus", morpheusFontFamily.Name);

                // Get the MPQ font 'Skurri'
                FontFamily skurriFontfamily = frameXmlFontService.GetFont(mpqSkurriFontPath);

                Assert.IsNotNull(skurriFontfamily, "GetFont returned null.");
                Assert.AreEqual("Skurri", skurriFontfamily.Name);
            }
        }

        [Test]
        public void GetFontsTest()
        {
            using (var mpqArchive = new MpqArchive(mpqArchivePath))
            using (var resourceFileLoader = new ResourceFileLoader())
            using (var frameXmlFontService = new FrameXmlFontService(resourceFileLoader))
            {
                // Add WoW base path
                resourceFileLoader.BasePaths.Add(Settings.Default.WowBasePath);

                // Add MPQ archive
                resourceFileLoader.MpqArchives.Add(mpqArchive);

                // Get the MPQ font 'Morpheus'
                FontFamily morpheusFontFamily = frameXmlFontService.GetFont(mpqMorpheusFontPath);

                // Get the MPQ font 'Skurri'
                FontFamily skurriFontfamily = frameXmlFontService.GetFont(mpqSkurriFontPath);

                // Get the MPQ font 'Morpheus' again
                FontFamily morpheusFontFamily2 = frameXmlFontService.GetFont(mpqMorpheusFontPath);

                // Get the MPQ font 'Skurri' again
                FontFamily skurriFontfamily2 = frameXmlFontService.GetFont(mpqSkurriFontPath);

                // See if the subsequently requested fonts are the same
                Assert.AreEqual(morpheusFontFamily, morpheusFontFamily2, "Subsequent calls to GetFont for the same font returned different instances.");
                Assert.AreEqual(skurriFontfamily, skurriFontfamily2, "Subsequent calls to GetFont for the same font returned different instances.");

                Assert.AreEqual(morpheusFontFamily, morpheusFontFamily2, "Subsequent calls to GetFont for the same font returned different instances.");
            }
        }

        [Test]
        public void GetNonExistentFontTest()
        {
            using (var mpqArchive = new MpqArchive(mpqArchivePath))
            using (var resourceFileLoader = new ResourceFileLoader())
            using (var frameXmlFontService = new FrameXmlFontService(resourceFileLoader))
            {
                // Add WoW base path
                resourceFileLoader.BasePaths.Add(Settings.Default.WowBasePath);

                // Add MPQ archive
                resourceFileLoader.MpqArchives.Add(mpqArchive);

                // Get non-existent font family
                FontFamily nonExistentFontFamily = frameXmlFontService.GetFont(@"Fonts\ComicSans.ttf");

                // Assert returned font family
                Assert.IsNull(nonExistentFontFamily, "GetFont returned font for non-existent font family.");
            }
        }

        [Test]
        public void GetNoExistentFontAfterGetFontTest()
        {
            using (var mpqArchive = new MpqArchive(mpqArchivePath))
            using (var resourceFileLoader = new ResourceFileLoader())
            using (var frameXmlFontService = new FrameXmlFontService(resourceFileLoader))
            {
                // Add WoW base path
                resourceFileLoader.BasePaths.Add(Settings.Default.WowBasePath);

                // Add MPQ archive
                resourceFileLoader.MpqArchives.Add(mpqArchive);

                // Get the MPQ font 'Morpheus'
                FontFamily morpheusFontFamily = frameXmlFontService.GetFont(mpqMorpheusFontPath);

                // Get non-existent font family
                FontFamily nonExistentFontFamily = frameXmlFontService.GetFont(@"Fonts\ComicSans.ttf");

                // Assert returned font family
                Assert.IsNotNull(morpheusFontFamily, "GetFont returned null for valid MPQ font family.");
                Assert.IsNull(nonExistentFontFamily, "GetFont returned font for non-existent font family.");
            }
        }
    }
}
